<?php

namespace XWAM\Addon\PXCallSystem\Controller;

/**
 * 费率控制器
 *
 * @since 1.0 2014-6-19 by sutroon
 */
class PayFeeController extends AppbaseController {

    public function index() {
        $this->display();
    }

    /**
     * 获取json数据
     * @since 2.0 <2015-6-10> SoChishun get_json()重构为 get_payfee_list_fit_json
     */
    public function get_payfee_list_fit_json() {
        $arr = AddonD('Payfee')->scope_list('idtext');
        $this->ajaxReturn($arr);
    }

    /**
     * 获取列表JSON数据
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function get_payfee_list_json() {
        if (!$this->isSuperAdmin) {
            $where['siteID'] = $this->loginData['siteID'];
        }
        $list = AddonD('Payfee')->get_payfee_list($where);
        $this->ajaxReturn($list);
    }

    /**
     * 导出数据
     * @since 1.0 <2014-6-13> SoChishun Added.
     */
    public function payfee_export() {
        if (!$this->isSuperAdmin) {
            die('没有权限!');
        }
        $list = AddonD('Payfee')->scope('export')->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    /**
     * 显示项目编辑表单
     */
    public function item_edit($id = 0) {
        $M = M('tcti_payfeerate');
        if ($id) {
            $this->assign('data', $M->find($id));
        }
        if (!$id) {
            $this->assign('filter', $M->getField('payType', true));
        }
        $this->display();
    }

    /**
     * 保存项目
     */
    public function item_save() {
        $params = new \Org\Su\Params();
        $params->addParam('payType', 'number', '费率组必须选择!');
        $params->addParam('feeName', 'require', '费率组名称必须填写!');
        $params->addParam('firstSec', 'require', '首次计费时长必须填写!');
        $params->addParam('firstRate', 'require', '首次计费金额必须填写!');
        $params->addParam('perSec', 'require', '间隔计费时长必须填写!');
        $params->addParam('perRate', 'require', '间隔计费金额必须填写!');
        if ($params->hasError()) {
            $this->ajaxMsg(false, $params->getError());
        }
        $id = I('id');
        $M = M('tcti_payfeerate');
        if ($id) {
            $where['id'] = $id;
            $params->removeParam('payType');
            $result = $M->where($where)->save($params->getParams());
        } else {
            if ($M->where('payType=' . I('payType'))->count('id') > 0) {
                $this->ajaxMsg(false, '费率组已存在!');
            }
            $params->addData('createdTime', date('Y-m-d H:i:s'));
            $result = $M->add($params->getParams());
        }
        $this->ajaxMsg($result, '保存{%}!');
    }

    /**
     * 删除项目
     */
    public function item_delete($id = 0) {
        if (!$id) {
            $this->ajaxMsg(false, '编号丢失!');
        }
        $this->ajaxMsg(M('tcti_payfeerate')->delete($id), '删除{%}!');
    }

}
